package nc.ui.cad.contractinfo;

import java.util.HashMap;
import java.util.Map;

import nc.bs.trade.business.HYPubBO;
import nc.ui.capub.baseui.ISingleCardController;
import nc.ui.capub.baseui.SingleCardBaseUI;
import nc.ui.pub.linkoperate.ILinkAdd;
import nc.ui.pub.linkoperate.ILinkAddData;
import nc.ui.trade.card.CardEventHandler;
import nc.uif.pub.exception.UifException;
import nc.vo.pub.SuperVO;

public class ContractinfoUI extends SingleCardBaseUI implements ILinkAdd  {

	
	private Map<String,Object> params;
	
	
	private HYPubBO bo=new HYPubBO();
	
	
	@Override
	public ISingleCardController createController() {
		return new ContractinfoCtrl();
	}
	
	@Override
	public CardEventHandler createEventHandler() {
		return new ContractinfoEventHandler(this, this.getUIControl());
	}

	@SuppressWarnings("unchecked")
	public void doAddAction(ILinkAddData adddata) {
		
		Map<String,Object> params=(HashMap<String,Object>)
		adddata.getUserObject();
		setUserObject(params);
		this.params=params;
		try {
			initAllData(params);
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
	
	public void initBodyData() throws Exception {

		String where=" pk_foreast='"+getCashForecastVO()
		.getAttributeValue("pk_cashforecast")+"' ";
		
		SuperVO queryVos[] = queryHeadVOs(where);
		getBufferData().clear();
		addDataToBuffer(queryVos);
		updateBuffer();
		
	}
	
	public void initData(){
		return;
	}
	
	public void initAllData(){
		initAllData(params);
	}
	
	
	
	private void initAllData(Map<String, Object> params) {
		
		try{
			
			initBodyData();
//			
			SuperVO vo=(SuperVO)params.get("cashforecast");
//			
			getBillCardPanel().setHeadItem("pk_contract",
			vo.getAttributeValue("pk_contract").toString());
			
			getBillCardPanel().setHeadItem("custname",
			getContractCustName(vo.getAttributeValue("pk_contract")
			.toString()));
			
			getBillCardPanel().execHeadLoadFormulas();
			
			
		}catch(Exception e){
			e.printStackTrace();
		}
		
	}
	
	private String getContractCustName(String conno) {
		try {
			
			String custcode=bo.findColValue("caa_doc_contract","a_custno",
			" a_conno='"+conno+"' and isnull(dr,0)=0 ").toString();
			
			return bo.findColValue("v_caa_cust","custname",
				   "custcode='"+custcode+"'").toString();
			
		} catch (UifException e) {
			return null;
		}
	}

	private SuperVO getCashForecastVO(){
		return (SuperVO)params.get("cashforecast");
	}
	
	
	

}
